﻿using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Xml;
using LibGit2Sharp;


namespace WeGet;
public class Apps
{
    public Apps(string DataDirectory)
    {
        RepositoryParentDirectory = DataDirectory;
        RepositoryDirectory = DataDirectory + $"{SlashForOS()}check-for-updates{SlashForOS()}CheckForUpdates.xml";
    }


    public string RepositoryDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + $"{SlashForOS()}check-for-updates{SlashForOS()}CheckForUpdates.xml";
    public string RepositoryParentDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    public void Install(string PackageName)
    {
        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.Load(RepositoryDirectory);

        Repository.Clone(xmlDocument.SelectSingleNode($"apps/{PackageName}/channels/channel").Attributes["link"].Value, Environment.CurrentDirectory + SlashForOS() + PackageName + "-publish");
    }
    public void Install(string PackageName, Channel channel, string ChannelIdentity)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(RepositoryDirectory);
        foreach (XmlNode item in xmlDocument.SelectSingleNode($"apps/{PackageName}/channels"))
        {
            if (item.Attributes["identity"].Value == ChannelIdentity)
            {
                Repository.Clone(item.Attributes["link"].Value, Environment.CurrentDirectory + SlashForOS() + PackageName + "-" + channel.ToString().ToLower());
            }
        }
    }
    public void Remove(string PackageName)
    {
        string[] readyToDelete = Directory.GetFiles(Environment.CurrentDirectory + SlashForOS() + PackageName + "-publish", "*", SearchOption.AllDirectories);
        foreach (var readyToDeleteStr in readyToDelete)
        {
            FileInfo fileInfo = new FileInfo(readyToDeleteStr);
            fileInfo.Attributes = FileAttributes.Normal;
        }
        try
        {
            Directory.Delete(Environment.CurrentDirectory + SlashForOS() + PackageName + "-publish" + $"{SlashForOS()}.git", true);
        }
        catch { }

        DirectoryInfo directoryInfo1 = new DirectoryInfo(Environment.CurrentDirectory + @"\" + PackageName + "-publish");
        directoryInfo1.Delete(true);
    }
    public void Update()
    {
        if (Directory.Exists(RepositoryParentDirectory + $"{SlashForOS()}check-for-updates{SlashForOS()}"))
        {
            Environment.CurrentDirectory = RepositoryParentDirectory;
            Commands.Pull(new Repository("check-for-updates"), new Signature("Name", "Email", DateTime.Now), null);
        }
        else
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(RepositoryParentDirectory + $"{SlashForOS()}Settings.xml");
            string repo = xmlDocument.SelectSingleNode("/Settings/Repo").InnerText;
            Repository.Clone(repo, RepositoryParentDirectory + $"{SlashForOS()}check-for-updates");

        }
    }
    public void Upgrade(string PackageName)
    {
        PullOptions pullOptions = new PullOptions();
        pullOptions.FetchOptions = new FetchOptions();

        Commands.Pull(new Repository($"{Environment.CurrentDirectory}\\{PackageName}-publish"), new Signature("Name", "Email", DateTime.Now), null);

    }
    public static void Clone(string Repo)
    {
        Repository.Clone(Repo, Environment.CurrentDirectory + Repo);

    }
    public static void Pull(string Repo)
    {
        Commands.Pull(new Repository(Repo), new Signature("Name", "Email", DateTime.Now), null);
    }
    static string SlashForOS()
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            return "\\";
        }
        else
        {
            return "/";
        }
    }
}
